package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.ProductInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.ProductInfoMapper;
import com.xjtu.duansheli.service.ProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> getAllProduct(int current, int size) {
        Page<ProductInfo> page = new Page<>(current,size);
        productInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
//        List<ProductInfo> productInfos = productInfoMapper.selectList(null);
//        if(productInfos.size() == 0){
//            throw new MyException(500,"数据库中无数据！");
//        }return productInfos;
    }

    @Override
    public ProductInfo getProductByid(Long pid) {
        ProductInfo productInfo = productInfoMapper.selectById(pid);
        if(productInfo == null){
            throw new MyException(500,"不存在该商品！");
        }
        return productInfo;
    }

    @Override
    public int addProduct(ProductInfo product) {
        try{
            return productInfoMapper.insert(product);
        }catch (Exception e){
            e.printStackTrace();
            throw new MyException(500,"添加商品失败！");
        }
    }

    @Override
    public int updateProduct(ProductInfo product) {
        try{
            return productInfoMapper.updateById(product);
        }catch (Exception e){
            e.printStackTrace();
            throw new MyException(500,"更新商品失败！");
        }
    }

    @Override
    public int deleteProduct(Long pid) {
        try{
            return productInfoMapper.deleteById(pid);
        }catch (Exception e){
            throw new MyException(500,"删除商品失败！");
        }
    }

    @Override
    public List<ProductInfo> getProductBysaleid(Long saleid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("sale_id",saleid);

        List<ProductInfo> productInfos = productInfoMapper.selectByMap(map);
        if(productInfos.size() == 0){
            throw new MyException(500,"该用户未发布任何商品！");
        }
        return productInfos;
    }

    @Override
    public int getTotal() {
        return productInfoMapper.selectCount(null);
    }

    @Override
    public List<ProductInfo> searchProductByKey(String pName) {
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        try{
            queryWrapper.like(StringUtils.isNotBlank(pName), "pname", pName);
            List<ProductInfo> productInfos = productInfoMapper.selectList(queryWrapper);
            return productInfos;
        }catch (Exception e){
            throw new MyException(400,"搜索失败");
        }
    }
}
